<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>Selenium驱动 | Huafeihua</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="##1 验证驱动可以将驱动放到一个文件夹，添加到环境变量from selenium import webdriver driver = webdriver.Firefox()driver = webdriver.Chrome()driver = webdriver.Ie()driver = webdriver.Edge()driver = webdriver.Opera() ##2 定位方法idn">
<meta name="keywords" content="Selenium">
<meta property="og:type" content="article">
<meta property="og:title" content="Selenium驱动">
<meta property="og:url" content="http://malone00.gitee.io/huafeihua/2019/01/22/自动化/Selenium-驱动/index.html">
<meta property="og:site_name" content="Huafeihua">
<meta property="og:description" content="##1 验证驱动可以将驱动放到一个文件夹，添加到环境变量from selenium import webdriver driver = webdriver.Firefox()driver = webdriver.Chrome()driver = webdriver.Ie()driver = webdriver.Edge()driver = webdriver.Opera() ##2 定位方法idn">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-01-22T09:05:16.246Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Selenium驱动">
<meta name="twitter:description" content="##1 验证驱动可以将驱动放到一个文件夹，添加到环境变量from selenium import webdriver driver = webdriver.Firefox()driver = webdriver.Chrome()driver = webdriver.Ie()driver = webdriver.Edge()driver = webdriver.Opera() ##2 定位方法idn">
  
    <link rel="alternate" href="/huafeihua/atom.xml" title="Huafeihua" type="application/atom+xml">
  
  
    <link rel="icon" href="/images/tmp.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/huafeihua/css/style.css">
  <link rel="stylesheet" href="/huafeihua/css/highlight.css">
</head>
</html>
<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" title="图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/huafeihua/" title="">首页</a>
      
        <a class="nav-item" href="/huafeihua/categories" title="">分类</a>
      
        <a class="nav-item" href="/huafeihua/archives" title="">归档</a>
      
        <a class="nav-item" href="/huafeihua/tags" title="">标签</a>
      
        <a class="nav-item" href="/huafeihua/about" title="">关于</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-自动化/Selenium-驱动" class="article global-container article-type-post" itemscope="" itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      Selenium驱动
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/huafeihua/2019/01/22/自动化/Selenium-驱动/" class="article-date">
  <time datetime="2019-01-22T08:58:59.000Z" itemprop="datePublished">2019-01-22</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/huafeihua/categories/自动化/">自动化</a>
  </div>

    
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/huafeihua/tags/Selenium/">Selenium</a></li></ul>

  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-doorframe" itemprop="articleBody">
      
        <p>##1 验证驱动<br>可以将驱动放到一个文件夹，添加到环境变量<br>from selenium import webdriver</p>
<p>driver = webdriver.Firefox()<br>driver = webdriver.Chrome()<br>driver = webdriver.Ie()<br>driver = webdriver.Edge()<br>driver = webdriver.Opera()</p>
<p>##2 定位方法<br>id<br>name<br>class name<br>tag name<br>link text<br>partial link text<br>xpath<br>css selector<br>这8种定位方式在Python selenium中所对应的方法为：</p>
<p>find_element_by_id()<br>find_element_by_name()<br>find_element_by_class_name()<br>find_element_by_tag_name()<br>find_element_by_link_text()<br>find_element_by_partial_link_text()<br>find_element_by_xpath()<br>find_element_by_css_selector()</p>
<p>##3 控制浏览器<br>set_window_size(400,800)<br>set_window_size()<br>driver.back()<br>driver.forward()<br>driver.refresh() #刷新</p>
<p>clear()<br>send_keys()<br>click()<br>ele.submit()</p>
<p>##4其他<br>size  返回尺寸<br>text 获取文本<br>get_attribute(name) 获取属性<br>is_displayed()  是否用户可见</p>
<p>##鼠标事件<br>from selenium.webdriver import ActionChains<br>ActionChains(driver).<em>**</em></p>
<p>context_click()  右击<br>double_click()  双击<br>drag_and_trop() 拖动<br>move_to_element() 悬停</p>
<p>##键盘<br>from selenium.webdriver.common.keys import Keys<br>Keys.BACK_SPACE<br>Keys.CONTROL, ‘a’    (c, v)<br>Keys.ENTER<br>send_keys(Keys.BACK_SPACE) 删除键（BackSpace）</p>
<p>send_keys(Keys.SPACE) 空格键(Space)</p>
<p>send_keys(Keys.TAB) 制表键(Tab)</p>
<p>send_keys(Keys.ESCAPE) 回退键（Esc）</p>
<p>send_keys(Keys.ENTER) 回车键（Enter）</p>
<p>send_keys(Keys.CONTROL,‘a’) 全选（Ctrl+A）</p>
<p>send_keys(Keys.CONTROL,‘c’) 复制（Ctrl+C）</p>
<p>send_keys(Keys.CONTROL,‘x’) 剪切（Ctrl+X）</p>
<p>send_keys(Keys.CONTROL,‘v’) 粘贴（Ctrl+V）</p>
<p>send_keys(Keys.F1) 键盘 F1</p>
<p>……</p>
<p>send_keys(Keys.F12) 键盘 F12</p>
<p>##等待<br>显式<br>WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)</p>
<p>driver ：浏览器驱动。<br>timeout ：最长超时时间，默认以秒为单位。<br>poll_frequency ：检测的间隔（步长）时间，默认为0.5S。<br>ignored_exceptions ：超时后的异常信息，默认情况下抛NoSuchElementException异常。<br>WebDriverWait()一般由until()或until_not()方法配合使用，下面是until()和until_not()方法的说明。 <em> until(method, message=‘’) 调用该方法提供的驱动程序作为一个参数，直到返回值为True。 </em> until_not(method, message=‘’) 调用该方法提供的驱动程序作为一个参数，直到返回值为False。</p>
<p>在本例中，通过as关键字将expected_conditions 重命名为EC，并调用presence_of_element_located()方法判断元素是否存在<br>隐式</p>
<h3 id="设置隐式等待为10秒"><a href="#设置隐式等待为10秒" class="headerlink" title="设置隐式等待为10秒"></a>设置隐式等待为10秒</h3><p>driver.implicitly_wait(10)</p>
<p>switch_to.frame()  id或name<br>也可以先定位对象 把对象传给frame<br>driver.switch_to.parent_frame()<br>switch_to.default_content()  跳回到最外层</p>
<p>###警告框<br>driver.switch_to.alert.accept()</p>
<p>text：返回 alert/confirm/prompt 中的文字信息。</p>
<p>accept()：接受现有警告框。</p>
<p>dismiss()：解散现有警告框。</p>
<p>send_keys(keysToSend)：发送文本至警告框。keysToSend：将文本发送至警告框。</p>
<p>###下拉选择框<br>from selenium.webdriver.support.select improt Select<br>s_ele = webdriver.find_ele<br>Select(s_ele).select_by_value(value)</p>
<p>###上传文件<br>input<br>ele = find<br>ele.send_keys(文件)</p>
<p>###cookie处理<br>WebDriver操作cookie的方法：</p>
<p>get_cookies()： 获得所有cookie信息。</p>
<p>get_cookie(name)： 返回字典的key为“name”的cookie信息。</p>
<p>add_cookie(cookie_dict) ： 添加cookie。“cookie_dict”指字典对象，必须有name 和value 值。</p>
<p>delete_cookie(name,optionsString)：删除cookie信息。“name”是要删除的cookie的名称，“optionsString”是该cookie的选项，目前支持的选项包括“路径”，“域”。</p>
<p>delete_all_cookies()： 删除所有cookie信息。</p>
<p>###调用js<br>控制滚动条移动<br>js=”window.scrollTo(100,450);”<br>driver.execute_script(js)<br>sleep(3)</p>
<p>###保存图片<br>path = r’D:\shot.png’<br>方法：driver.get_screenshot_as_file(path)</p>
<p>##关闭窗口</p>
<ul>
<li>close()  关闭当前</li>
<li>quit()  退出驱动，关闭所有窗口</li>
</ul>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/huafeihua/2019/01/22/自动化/webdriver-清除浏览器缓存操作/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            webdriver-清除浏览器缓存
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/huafeihua/2019/01/22/flask/WTForms支持的HTML标准字段/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">flask-WTForms支持的HTML标准字段</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
<div id="gitmentContainer"></div>
<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  owner: 'malone00',
  repo: 'https://gitee.com/malone00/huafeihua.git',
  oauth: {
    client_id: '61b3470a9440579b58b3',
    client_secret: '889ecdcc18f7687dd9f5b36feb85080baf8a6c48',
  },
})
gitment.render('gitmentContainer')
</script>

  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box">
    <img class="avatar" src="/huafeihua/images/default-avatar.jpeg" title="图片来自网络">
    <h3 class="avatar-name">
      
        听风看雨
      
    </h3>
    <p class="avatar-slogan">
      抗揍的愤青，半个IT男。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/Linux/">Linux</a></li><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/python/">python</a></li><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/入坑/">入坑</a></li><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/技巧/">技巧</a></li><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/自动化/">自动化</a></li><li class="category-list-item"><a class="category-list-link" href="/huafeihua/categories/项目管理/">项目管理</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Appium/">Appium</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Docker/">Docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Jenkins/">Jenkins</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Nginx/">Nginx</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Selenium/">Selenium</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/Ubuntu/">Ubuntu</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/centos/">centos</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/flask/">flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/rabbit/">rabbit</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/sql/">sql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/ubuntu/">ubuntu</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/效率/">效率</a></li><li class="tag-list-item"><a class="tag-list-link" href="/huafeihua/tags/爬虫/">爬虫</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/huafeihua/tags/Appium/" style="font-size: 10px;">Appium</a> <a href="/huafeihua/tags/Docker/" style="font-size: 13.33px;">Docker</a> <a href="/huafeihua/tags/Jenkins/" style="font-size: 10px;">Jenkins</a> <a href="/huafeihua/tags/Nginx/" style="font-size: 10px;">Nginx</a> <a href="/huafeihua/tags/Selenium/" style="font-size: 13.33px;">Selenium</a> <a href="/huafeihua/tags/Ubuntu/" style="font-size: 10px;">Ubuntu</a> <a href="/huafeihua/tags/centos/" style="font-size: 16.67px;">centos</a> <a href="/huafeihua/tags/flask/" style="font-size: 20px;">flask</a> <a href="/huafeihua/tags/python/" style="font-size: 20px;">python</a> <a href="/huafeihua/tags/rabbit/" style="font-size: 10px;">rabbit</a> <a href="/huafeihua/tags/sql/" style="font-size: 10px;">sql</a> <a href="/huafeihua/tags/ubuntu/" style="font-size: 10px;">ubuntu</a> <a href="/huafeihua/tags/效率/" style="font-size: 10px;">效率</a> <a href="/huafeihua/tags/爬虫/" style="font-size: 10px;">爬虫</a>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/huafeihua/archives/2019/01/">一月 2019</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/huafeihua/2019/01/29/python-mq/">python rabbitmq</a>
          </li>
        
          <li>
            <a href="/huafeihua/2019/01/22/python/Unittest/">Python-Unittest</a>
          </li>
        
          <li>
            <a href="/huafeihua/2019/01/22/项目管理/项目管理2018-07-23/">U项目管理</a>
          </li>
        
          <li>
            <a href="/huafeihua/2019/01/22/随笔/-Kanboard-安装之Windows/">Kanboard 安装之Windows</a>
          </li>
        
          <li>
            <a href="/huafeihua/2019/01/22/运维部署/单网卡实现内外网访问/">单网卡实现内外网访问</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box global-width">
    &copy; 2019 Edwin Ma &nbsp;&nbsp;
    驱动于 <a href="http://hexo.io/" target="_blank">Hexo</a><br>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script src="/js/jquery-2.0.3.min.js"></script>

  <link rel="stylesheet" href="/huafeihua/fancybox/jquery.fancybox.css">
  <script src="/huafeihua/fancybox/jquery.fancybox.pack.js"></script>


<script src="/huafeihua/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/huafeihua/" class="mobile-nav-link">首页</a>
  
    <a href="/huafeihua/categories" class="mobile-nav-link">分类</a>
  
    <a href="/huafeihua/archives" class="mobile-nav-link">归档</a>
  
    <a href="/huafeihua/tags" class="mobile-nav-link">标签</a>
  
    <a href="/huafeihua/about" class="mobile-nav-link">关于</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>